using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace libACO
{
    static class libACOAux
    {
        static string mLastError = "";

        public static string LastError
        {
            get { return mLastError; }
        }

        public static bool ImpAddFiles(string ArchivePath, List<string> FilesToAdd)
        {
            if (!File.Exists(ArchivePath)) return false;

            List<string> mFiles = new List<string>();

            ACOarchive mArc = new ACOarchive(ArchivePath);
            string mPath = Path.GetTempFileName();
            File.Delete(mPath);
            Directory.CreateDirectory(mPath);
            mArc.UnpackTo(mPath);
            string[] arcFiles = mArc.FileNames;

            foreach (string sFile in arcFiles)
            {
                mFiles.Add(mPath + "\\" + sFile);
            }

            foreach (string sFile in FilesToAdd)
            {
                if (File.Exists(sFile))
                {
                    if (mFiles.Contains(mPath + "\\" + Path.GetFileName(sFile)))
                    {
                        mFiles.Remove(mPath + "\\" + Path.GetFileName(sFile));
                    }
                    mFiles.Add(sFile);
                }
            }

            string tArc = Path.GetTempFileName();
            File.Delete(tArc);

            ACOarchive nArc = new ACOarchive(tArc, mFiles);

            if (nArc == null)
            {
                return false;
            }

            string backArc = "";
            try
            {
                backArc = Path.GetTempFileName();
                File.Delete(backArc);
                File.Move(ArchivePath, backArc);
                //File.Delete(ArchivePath);
                File.Move(tArc, ArchivePath);

                Directory.Delete(mPath, true);
                File.Delete(backArc);
                return true;
            }
            catch (Exception ex)
            {
                mLastError = ex.Message;
                if (File.Exists(backArc))
                {
                    File.Move(backArc, ArchivePath);
                }
                return false;
            }
        }

        public static bool ImpDeleteFiles(string ArchivePath, List<string> FilesToDelete)
        {
            if (!File.Exists(ArchivePath)) return false;

            List<string> mFiles = new List<string>();

            ACOarchive mArc = new ACOarchive(ArchivePath);
            string mPath = Path.GetTempFileName();
            File.Delete(mPath);
            Directory.CreateDirectory(mPath);
            mArc.UnpackTo(mPath);
            string[] arcFiles = mArc.FileNames;

            foreach (string sFile in arcFiles)
            {
                mFiles.Add(mPath + "\\" + sFile);
            }

            foreach (string sFile in FilesToDelete)
            {
                if (mFiles.Contains(mPath + "\\" + Path.GetFileName(sFile)))
                {
                    mFiles.Remove(mPath + "\\" + Path.GetFileName(sFile));
                }
            }

            string tArc = Path.GetTempFileName();
            File.Delete(tArc);

            ACOarchive nArc = new ACOarchive(tArc, mFiles);

            if (nArc == null)
            {
                return false;
            }

            string backArc = "";
            try
            {
                backArc = Path.GetTempFileName();
                File.Delete(backArc);
                File.Move(ArchivePath, backArc);
                //File.Delete(ArchivePath);
                File.Move(tArc, ArchivePath);

                Directory.Delete(mPath, true);
                File.Delete(backArc);
                return true;
            }
            catch (Exception ex)
            {
                mLastError = ex.Message;
                if (File.Exists(backArc))
                {
                    File.Move(backArc, ArchivePath);
                }
                return false;
            }
        }
    }
}
